//
//  MyViewController.m
//  Restaurant
//
//  Created by wanghuan on 15/4/27.
//  Copyright (c) 2015年 wanghuan. All rights reserved.
//

#import "MyViewController.h"

@interface MyViewController ()

@property(nonatomic,strong) NSUserDefaults *defaults;

@end

@implementation MyViewController


- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    
     self.tabBarController.tabBar.hidden = NO;
    
    _defaults = [NSUserDefaults standardUserDefaults];
    
    self.view.backgroundColor = AppBackGroundColor;
    
    if (_myTableView) {
        
        [_myTableView removeFromSuperview];
    }
    
    _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, APPWith, APPHeight-64-49) style:UITableViewStyleGrouped];
    
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    
    _myTableView.backgroundColor = AppBackGroundColor;
    
    //让_myTableView.backgroundColor 和 _myTableView.separatorColor 的颜色相同 这样就可以按钮在cell上如何取消分割线的问题
    _myTableView.separatorColor = _myTableView.backgroundColor;
    
    [self.view addSubview:_myTableView];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    
    
}

#pragma mark - UITableViewDataSource,UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 2) {
        return 2;
    }
    
    return 1;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    if ([userDefaults objectForKey:usersId]) {
        
        return 4;
        
    }
    
    return 3;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (!indexPath.section) {
        
        return 80;
        
    }
    
    if (indexPath.section == 3) {
        
        return 80;
    }
    
    return 44;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 20;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    if (!section) {
        
        return 0.1;
        
    }
    
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (!indexPath.section) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell1"];
        }
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIButton *HeaderimageView = [[UIButton alloc]initWithFrame:CGRectMake(25, 5, 70, 70)];
        
        HeaderimageView.layer.cornerRadius = 35;
        
        HeaderimageView.layer.borderColor = [[UIColor whiteColor]CGColor];
        
        HeaderimageView.layer.borderWidth = 3;
        
        HeaderimageView.backgroundColor = [UIColor yellowColor];
        
       [cell.contentView addSubview:HeaderimageView];
        
        if ([[_defaults objectForKey:usersId] intValue]) {
        
            for (int i = 0; i < 2; i++) {
                
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(105, 10+i*30, 150, 30)];
                
                if (!i) {
                    
                    label.text = @"xxxxx";
                    
                    label.font = [UIFont systemFontOfSize:17];
                    
                } else {
                    
                    label.text = [_defaults objectForKey:Usertelephone];
                    
                    label.font = [UIFont systemFontOfSize:14];
                }
                
                [cell.contentView addSubview:label];
                
                
            }

        
        } else {
        
            
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(105,25 , 100, 30)];
            
            
            [button setTitle:@"点击登录" forState:UIControlStateNormal];
            
             button.tag = 2;
            
            [button addTarget:self action:@selector(LoginClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            [cell.contentView addSubview:button];
            
        }
        
        return cell;
        
    }
    
    if (indexPath.section == 1) {
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        
        
        
        if (!cell) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.textLabel.text = @"账户信息";
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        return cell;
        
    }
    
    if (indexPath.section == 2) {
        
        if (!indexPath.row) {
            
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3"];
            
            if (!cell) {
                
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell3"];
            }
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            cell.textLabel.text = @"帮助与反馈";
            
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
            return cell;
        }
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3"];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        if (!cell) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell3"];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.textLabel.text = @"关于我们";
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
        return cell;
    }
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell4"];
    
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell4"];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.backgroundColor = [UIColor clearColor];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(40, 40, APPWith-80, 40)];
    
    [button setBackgroundImage:[UIImage imageNamed:@"button1"] forState:UIControlStateNormal];
    
    [button setTitle:NSLocalizedString(@"退出登录", nil) forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(Leavelogin) forControlEvents:UIControlEventTouchUpInside];
    
    [cell.contentView addSubview:button];
    
    return cell;




}

#pragma mark - 退出登录
- (void)Leavelogin {

    _manager = [[AFHTTPSessionManager alloc]init];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    [_manager GET:[NSString stringWithFormat:MyLeavelogin,[defaults objectForKey:usersId]] parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (![[responseObject objectForKey:@"success"] boolValue]) {
            
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"提示", nil) message:NSLocalizedString(@"退出失败", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"确定", nil) otherButtonTitles:nil, nil];
            
            [alter show];
            
            return ;
        }
        
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"提示", nil) message:NSLocalizedString(@"退出成功", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"确定", nil) otherButtonTitles:nil, nil];
        
        
        [alter show];
        
        [defaults removeObjectForKey:usersId];
        
        [defaults synchronize];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"提示", nil) message:NSLocalizedString(@"网络异常", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"确定", nil) otherButtonTitles:nil, nil];
        
        [alter show];
    }];
    

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
}

#pragma mark - 头像和登录的点击事件
- (void)LoginClick:(UIButton *)sender {

    if (sender.tag == 2) {
        
        LoginViewController *lvc = [[LoginViewController alloc]init];
        
        self.tabBarController.tabBar.hidden = YES;
        
        lvc.viewController = self;
        
        [self.navigationController pushViewController:lvc animated:YES];
    }

}

#pragma mark - 退出提示框确定按钮的点击事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (!buttonIndex) {
        
        NSString *appDomain = [[NSBundle mainBundle]bundleIdentifier];
        
        [_defaults removePersistentDomainForName:appDomain];
        
        
        [_defaults synchronize];
        
        [self viewWillAppear:YES];
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
